package the.workshop.tms.order.application.service;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import the.workshop.tms.order.infrastructure.commons.CargoTypeEnum;
import the.workshop.tms.order.infrastructure.commons.PayStatusEnum;
import the.workshop.tms.order.infrastructure.persistent.model.OrderDO;
import the.workshop.tms.order.application.service.dto.OrderImpExcelDTO;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderExcelService {

    @Resource
    private IOrderService orderService;

    public boolean impExcel(List<OrderImpExcelDTO> impExcelList) {

        if (CollectionUtils.isEmpty(impExcelList)) {
            return false;
        }

        List<OrderDO> orderList = impExcelList.stream().map(this::convertOf).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(orderList)) {
            return orderService.saveBatch(orderList, orderList.size());
        }

        return false;
    }

    private OrderDO convertOf(OrderImpExcelDTO source) {
        OrderDO target = new OrderDO();
        target.setDeliveryDate(source.getDeliveryDate());
        target.setDeliveryStatus(PayStatusEnum.getCode(source.getDeliveryStatusName()));
        target.setDeliveryShift(source.getDeliveryShift());
        target.setConsignorAddress(source.getConsignorAddress());
        target.setConsignorHouseNumber(source.getConsignorHouseNumber());
        target.setReceiverName(source.getReceiverName());
        target.setReceiverAddress(source.getReceiverAddress());
        target.setReceiverHouseNumber(source.getReceiverHouseNumber());
        target.setCargoType(CargoTypeEnum.getCode(source.getCargoTypeName()));
        target.setCargoNum(source.getCargoNum());
        target.setDriverName(source.getDriverName());
        target.setVehicleNumber(source.getVehicleNumber());
        target.setCarrierName(source.getCarrierName());
        target.setDriverFee(source.getDriverFee());
        target.setCarrierFee(source.getCarrierFee());
        target.setTotalFee(source.getTotalFee());
        target.setPayStatus(PayStatusEnum.getCode(source.getPayStatusName()));
        target.setRemark(source.getRemark());
        target.setMarketAmount(source.getMarketAmount());
        return target;
    }
}
